<?php
// +----------------------------------------------------------------------
// | sfadmin - Suifeng Administration
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://suifeng.tech All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: suifeng <1787585212@qq.com>
// +----------------------------------------------------------------------

namespace suifeng\builder\form\validator;

use suifeng\builder\form\Validator;

class Regex extends Validator
{
    /**
     * 错误信息。
     *
     * @var string
     */
    public $message;

    /**
     * 正则表达式。
     *
     * @var unknown
     */
    public $pattern;

    /**
     * 默认的表达式。
     *
     * @var array
     */
    protected $default_patterns = [
        'alpha'     =>  [
            '/^[A-Za-z]+$/',
            '{control.label} 只能包含字母。',
        ],
        'email'       =>  [
            '/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/',
            '{control.label} 必须是 EMAIL 格式。',
        ],
        'email'       =>  [
            '/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/',
            '{control.label} 必须是数字格式。',
        ],
        'url'       =>  [
            '/^[a-zA-z]+://[^\s]* 或 ^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$/',
            '{control.label} 必须是 URL 格式。',
        ],
        'domain'       =>  [
            '/^[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(/.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+/.?$/',
            '{control.label} 必须是 DOMAIN 格式。',
        ],
        'mobile'       =>  [
            '/^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/',
            '{control.label} 必须是手机号码。',
        ],
        'phone'       =>  [
            '/^\d{3}-\d{8}|\d{4}-\d{7}$/',
            '{control.label} 必须是电话号码。',
        ],
        'idcard'       =>  [
            '/^\d{15}|\d{18}$/',
            '{control.label} 必须是身份证号码。',
        ],
        'username'       =>  [
            '/^[a-zA-Z][a-zA-Z0-9_]{4,15}$/',
            '{control.label} 必须是用户名。',
        ],
        'password'       =>  [
            '/^[a-zA-Z]\w{5,17}$/',
            '{control.label} 必须是密码。',
        ],
        'date'       =>  [
            '/^\d{4}-\d{1,2}-\d{1,2}$/',
            '{control.label} 必须是日期格式。',
        ],
    ];

    /**
     * 验证方法。
     */
    protected function _validate_core()
    {
        if (isset($this->default_patterns[$this->pattern])) {
            $pattern = $this->default_patterns[$this->pattern][0];
        } else {
            $pattern = $this->pattern;
        }

        $value = $this->_get_control_value();

        if (! empty($pattern) && ! preg_match($pattern, $value)) {
            if (empty($this->message) && isset($this->default_patterns[$this->pattern])) {
                $message = $this->default_messages[$this->logic][1];
            } else {
                $message = $this->message;
            }

            $this->errors[] = $this->_format_message($message, array('value' => $value));
        }
    }
}
